package com.lx.spring.config.config;

import com.lx.spring.book.service.itf.BookService;
import com.lx.spring.config.bean.*;
import com.lx.spring.config.condition.LinuxCondition;
import com.lx.spring.config.condition.MyImportBeanDefinitionRegistrar;
import com.lx.spring.config.condition.MyImportSelector;
import com.lx.spring.config.condition.WindowsCondition;
import com.lx.spring.config.filter.MyTypeFilter;
import com.lx.spring.context.demo.Red;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScans(
        value = {
                @ComponentScan(value = "com.lx.spring"
                        , includeFilters = {
//                        @ComponentScan.Filter(type=FilterType.ANNOTATION, classes = Controller.class),
//                        @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, classes = BookService.class)
//                        @ComponentScan.Filter(type= FilterType.REGEX, pattern = "com.lx.spring.book.dao.BookDAO")
                        @ComponentScan.Filter(type= FilterType.CUSTOM, classes = MyTypeFilter.class),
                        @ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE, classes = {Dog.class, MyBeanPostProcessor.class})
                }
                , useDefaultFilters = false)
        }
)
//使用import 快速导入组件
@Import({Color.class, Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
// ImportSelector:返回需要导入的组件的全类名(数组)

public class MainConfig {

    @Lazy(value = true)
    @Scope(value = "singleton")
    //默认用方法名作为bean的id，除非自己指定bean的id
    @Bean(name="person")
    @Conditional(value = {WindowsCondition.class})
    public Person person(){
        return new Person("lisi", 30);
    }



    @Bean
    public ColorFactoryBean colorFactoryBean(){
        return new ColorFactoryBean();
    }

    @Scope("singleton")// 单实例时容器管理bean的生命周期
//    @Scope("prototype")//多实例时容器不会管理bean,只负责在需要的时候创建
    @Bean(initMethod = "init", destroyMethod = "destroy1")
    public Car car(){
        return new Car();
    }


}
